<?php


namespace app\common\model;


use app\common\traites\TraitModel;
use think\Model;

class GameRecords extends Model
{
    use TraitModel;

    public $name = 'dianji_records';
    public static $getCloseStatus = [1 => '待开牌', 2 => '已结算', 3 => '台面作废', 4 => '修改结果'];
    public static $getAgentStatus = [1 => '洗码结算', 2 => '未结算', 9 => '代理已结算'];//全部结算
    //下注
//    public function getStartTimeAttr($value)
//    {
//        if ($value == 0) return $value;
//        $status = date('Y-m-d H:i:s',$value);
//        return ['test'=>$status,'value'=>$value];
//    }

    public static function page_list($map, $limit, $page, $date)
    {
        $list = self::alias('a');
        if (isset($date['start']) && isset($date['end'])) {
            $list = $list->whereTime('a.created_at', 'between', [$date['start'], $date['end']]);
        } elseif (isset($date['start'])) {
            $list = $list->whereTime('a.created_at', '>=', $date['start']);
        } elseif (isset($date['end'])) {
            $list = $list->whereTime('a.created_at', '<=', $date['end']);
        }
        $where = self::whereMap('b.id');
        return $list->where($map)
            ->where($where)
            ->join('common_user b', 'a.user_id = b.id', 'left')
            ->join((new GameType())->name . ' c', 'a.game_type = c.id', 'left')
            ->field('a.*,b.user_name,c.type_name')
            ->order('id desc')
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->each(function ($item, $key) {

                if (strlen($item['detail']) > 5) {
                    $item['detail'] = explode('||', $item['detail'])[0];
                }
                $item->close_status_text = '';
                if (isset(self::$getCloseStatus[$item['close_status']])) {
                    return $item->close_status_text = self::$getCloseStatus[$item['close_status']];
                }
            });

    }

    //盈亏排行查询，只查询当日
    public static function profit_list($map, $limit, $page)
    {
        $model = self::alias('a');
        //得到查询的时间点
        $model = self::get_where_time_by($model, '', '', $map['type']);
        //查询用户是否被冻结，用于踢下线
        $model = $model->join('common_user b', 'a.user_id = b.id', 'left');
        //分组统计今日用户获得金额排行
        $model = $model->field('sum(delta_amt)  money,user_id,status,user_name,money_balance,money_total_recharge,money_total_withdraw')->group('user_id');
        return $model->order('money desc')->paginate(['list_rows' => $limit, 'page' => $page], false);
//            $model->each(function ($item, $key) {
//           if ($item['status'] == 1){
//               return $item->status_text = '正常';
//           }
//                return $item->status_text = '已冻结';
//        });
    }

    /**
     * /用户当前局免佣状态
     * @param $table_id /台座ID
     * @param $number /靴号铺号
     * @param $user /用户信息
     * @param $is_order /是否需要知道 是否下单过 下单判断免佣会用到
     * return 默认 is_exempt = 1; $is_exempt->is_exempt查出是0 还是1
     */
    public static function user_status_bureau_number_is_exempt($table_id, $number, $user, $is_order = false)
    {
        $is_exempt = self::where([
            'xue_number' => $number['xue_number'],
            'pu_number' => $number['pu_number'],
            'table_id' => $table_id,
            'user_id' => $user['id']
        ])
            ->whereTime('created_at', 'today')
            ->order('created_at desc')
            ->find();

        #------获取是否下单过开始 下单会用到 101表示没下单过
        if ($is_order == true && empty($is_exempt)) {
            return 101;
        }
        #------获取是否下单过结束

        #####获取当前用户当前局免佣状态
        if (!empty($is_exempt)) {
            return $is_exempt->is_exempt;
        }
        return 0;
        #####获取当前用户当前局免佣状态结束
    }

    //本局游戏总共下注金额
    public static function user_count_money_game_this($table_id, $xue_number, $peilv, $user)
    {
        $money = self::where([
            'xue_number' => $xue_number['xue_number'],
            'pu_number' => $xue_number['pu_number'],
            'table_id' => $table_id,
            'user_id' => $user['id'],
            'game_peilv_id' => $peilv
        ])
            ->whereTime('created_at', 'today')
            ->count('bet_amt');
        return $money;
    }
}